(function ($) {
	var onlinePull;
	var onlinePage = 1,Limit = 20;
	var onlinePulling = false;
	var onlineData;
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	
	//必修课--预约
	function getOnlineData(){
		if (onlinePulling) {
			onlinePull.endPullUpToRefresh()
			return;
		}
		onlinePulling = true
		setTimeout(function(){
			var dataArr = [{},{},{}]
			onlinePull.endPullUpToRefresh();
			if (onlinePage > 2) {
				onlinePull.endPullUpToRefresh(true);
			}
			updateUI(dataArr)
			onlinePage += 1;
			onlinePulling = false
		},800);
	}
	function updateUI(dataArr){
		var onlineUL = document.getElementById("onlineUL");
		if (onlinePage == 1) {
			onlineUL.innerHTML = ''
		}
		var isAllSel= false;
		if ($('#selectAllBtn')[0].getAttribute('class').indexOf('dhc-active') != -1) {
			isAllSel = true
		}
		for(var i=0; i<dataArr.length; i++){
			var html = '<div class="oneItem"><div class="tipText">姓名：</div><div class="contentText">周贤</div></div>'+
				'<div class="oneItem"><div class="tipText">工号：</div><div class="contentText">2075</div></div>'+
				'<div class="oneItem"><div class="tipText">病区：</div><div class="contentText">内分泌护理单元</div></div>'+
				'<div class="oneItem"><div class="tipText">考核日期：</div><div class="contentText">2021-07-01 08:34</div></div>'+
				'<div class="oneItem"><div class="tipText">分数：</div><div class="contentText scoreText">3</div></div>'+
				'<div class="oneSelect nurfont nur-check-none"></div>'+
				'<div class="mui-btn mui-btn-primary baseBtn toJudgeBtn">阅卷</div>'
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			if (isAllSel) {
				newLi.classList.add('dhc-active')
			}
			newLi.innerHTML = html
			onlineUL.appendChild(newLi)
			
			var toJudgeBtn = newLi.querySelector('.toJudgeBtn')
			if (toJudgeBtn) {
				toJudgeBtn.addEventListener('tap',toJudgeBtnTapped)
			}
			var oneSelect = newLi.querySelector('.oneSelect')
			if (oneSelect) {
				oneSelect.addEventListener('tap',oneSelectTapped)
			}
		}
	}
	function oneSelectTapped(){
		var parentNode = this.parentNode;
		if (parentNode.getAttribute('class').indexOf('dhc-active') != -1) {
			parentNode.classList.remove('dhc-active')
		}else{
			parentNode.classList.add('dhc-active')
			
		}
	}
	
	function toJudgeBtnTapped(){
		var webview = plus.webview.create('onlineJudge.html', 'onlineJudge', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{sourceData:{},pageType:''});
		webview.show('fade-in');
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		if (mui.os.plus) {
			var self = plus.webview.currentWebview()
			var sourceData = self.sourceData;
			$('.mui-title')[0].innerText = sourceData['title']
			// pageType = self.pageType;
			// fromPage = self.fromPage;
		}
		mui("body").on("tap", ".searchArea .mui-icon-clear", function() {
			if (document.getElementById('search').value != "") {
				document.getElementById('search').value = ""
			}
			resetRefresh()
		});
		var search = document.getElementById('search');
		search.addEventListener('input',function(){
			resetRefresh()
		});
		//全选
		mui(".bottomArea").on("tap", "#selectAllBtn",selectAllBtnTapped);
		//提交
		mui(".bottomArea").on("tap", "#submitBtn", function() {
			var count = $('#onlineUL')[0].querySelectorAll('li.dhc-active').length
			mui('li.dhc-active').each(function(index) {
				$('#onlineUL')[0].removeChild(this)
				if (count == index + 1) {
					mui.toast('提交成功')
					$('#selectAllBtn')[0].classList.remove('dhc-active');
					resetRefresh();
				}
			});
		});
		
		var onlineScroll = document.getElementById('onlineScroll');
		onlinePull = $(onlineScroll).pullToRefresh({
			up: {
				callback: function() {
					getOnlineData()
				}
			}
		});
		getOnlineData();
	}
	function selectAllBtnTapped(){
		var selectAllBtn = $('#selectAllBtn')[0]
		if (selectAllBtn.getAttribute('class').indexOf('dhc-active') != -1) {
			selectAllBtn.classList.remove('dhc-active')
			mui('.mui-table-view-cell').each(function() {
				this.classList.remove('dhc-active');
			});
		}else{
			selectAllBtn.classList.add('dhc-active')
			mui('.mui-table-view-cell').each(function() {
				this.classList.add('dhc-active');
			});
		}
	}

	function resetRefresh(){
		onlinePage = 1;
		onlineData = []
		if (onlinePull) {
			onlinePull.refresh(true)
		}
		getOnlineData();
		$('#onlineWrapper').scroll().scrollTo(0,0,100);
	}
})(mui);